package com.lb.bs.client.processor.handle.dynamics.create;

import com.lb.bs.client.annotation.SingletonMark;
import com.lb.bs.client.beans.LBGrayItemBean;
import com.lb.bs.client.beans.LBItemBean;
import com.lb.bs.client.repository.StoreCenter;
import com.lb.bs.client.processor.ProcessorHandleService;
import com.lb.bs.client.processor.beans.ProcessorDynamicHandlerBean;
import com.lb.bs.client.util.FieldUtil;
import com.lb.bs.client.util.KeyGenerateUtil;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-12-19 10:28
 * description:
 */
@SingletonMark
public class GrayCreateItemKeyProcessorHandle implements ProcessorHandleService<ProcessorDynamicHandlerBean> {

    @Override
    public void handleProcess(ProcessorDynamicHandlerBean handlerBean) {
        StoreCenter storeCenter = StoreCenter.getInstance();
        String grayItemKey = handlerBean.getKey();
        Object data = handlerBean.getData();
        Map<String, LBGrayItemBean> grayItemKeyMap = storeCenter.getGrayItemKeyMap();
        LBGrayItemBean grayItemBean = grayItemKeyMap.get(grayItemKey);
        if (grayItemBean == null) {
            return;
        }
        String groupName = grayItemBean.getGroupName();


        String itemKey = KeyGenerateUtil.getItemKeyByGrayItemKey(grayItemKey);

        LBItemBean itemBean = storeCenter.getItemKeyMap().get(itemKey);
        if (itemBean == null) {
            return;
        }
        Field field = itemBean.getField();
        if (field == null) {
            return;
        }

        Object grayValue = FieldUtil.transform(field, data);
        grayItemBean.setGrayValue(grayValue);
    }
}
